// Package database @program: account
//@author: edte
//@create: 2021-12-27 21:22
package database

import (
	log2 "account/log"
	"account/tools"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"strconv"

	"github.com/go-redis/redis/v8"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func UserLoginDb(ctx context.Context, userPhone string, password string) (tools.User, error) {
	mysqlPath := tools.GetEnv("MYSQL_PATH", "localhost:3306")
	mysqlUser := tools.GetEnv("MYSQL_USER", "root")
	mysqlPassword := tools.GetEnv("MYSQL_PASSWORD", "123456")
	log.Println(mysqlUser + ":" + mysqlPassword + "@tcp(" + mysqlPath + ")/users?charset=utf8&parseTime=True&loc=Local")
	db, err := gorm.Open(mysql.Open(mysqlUser+":"+mysqlPassword+"@tcp("+mysqlPath+")/users?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return tools.User{}, err
	}
	err = db.AutoMigrate(&tools.User{})
	// db, err := gorm.Open(mysql.Open("root:123456@tcp(localhost:3306)/users?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return tools.User{}, errors.New("failed to connect database")
	}
	tmpUser := tools.User{}
	db.Model(&tools.User{}).Where("user_phone = ?", userPhone).First(&tmpUser)
	if tmpUser.Id == 0 {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Warring", "Account does not exist.")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return tools.User{}, errors.New("account does not exist")
	}
	if tmpUser.UserPhone != userPhone || tmpUser.Password != password {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Warring", "The account or password is incorrect")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return tools.User{}, errors.New("the account or password is incorrect")
	}
	if tmpUser.Status == true {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Debug", "The account has been banned")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		return tools.User{}, errors.New("the account has been banned")
	}
	/*
			rdb := redis.NewClient(&redis.Options{
			Addr:     "localhost:6379",
			Password: "", // no password set
			DB:       0,  // use default DB
			//PoolSize: 500, // 连接池大小
		})
	*/
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       0,       // use default DB
		//PoolSize: 500, // 连接池大小
	})
	tmpjson, err := json.Marshal(tmpUser)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Warring", "The account has been banned")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		return tools.User{}, err
	}
	_, err = rdb.Set(ctx, strconv.Itoa(tmpUser.Id), tmpjson, -1).Result()
	if err != nil {
		writeLog, err := log2.WriteLog(context.Background(), "account", "UserLoginDb", userPhone, "Warring", "The account has been banned")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err != nil {
			fmt.Println("写入日志失败")
		}
		return tools.User{}, err
	}
	return tmpUser, err
}
